package youleche.com.sql.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import youleche.com.sql.business.BFieldType;
import youleche.com.sql.servlet.common.SuperHttpServlet;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class SFieldTypeServlet
 */
public class SFieldTypeServlet extends SuperHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SFieldTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//获取字段类型
				this.getFieldType(request, response);
				break;
		}
	}

	/**
	 * 获取字段类型
	 * @param request
	 * @param response
	 */
	private void getFieldType(HttpServletRequest request, HttpServletResponse response){
		BFieldType handler = new BFieldType();
		try {
			//super.response(response, this.convertListToJsonForFieldType(handler.getFieldTypeList()));
			JsonHelper.printObjectToJsonString(response, this.convertListToJsonForFieldType(handler.getFieldTypeList()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String convertListToJsonForFieldType(List<String> list){
		StringBuffer sb = new StringBuffer();
		for (String v : list) {
			sb.append("{typeId:'");
			sb.append(v);
			sb.append("',typeName:'");
			sb.append(v);
			sb.append("'},");
		}
		
		sb.delete(sb.length() - 1, sb.length());
		
		return sb.toString();
	}
}
